package rule_window

import (
	"file_explorer/common/util"
	"file_explorer/model"
	"file_explorer/view/packed_widgets"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func (window *RuleWindow) newRuleSeqTab(onSubmit func()) *container.TabItem {
	form := widget.NewForm()

	startEntry := packed_widgets.NewNumEntry(util.PtrInt(0), nil, nil)
	widthEntry := packed_widgets.NewNumEntry(util.PtrInt(1), nil, nil)
	patternEntry := widget.NewEntry()
	patternEntry.SetPlaceHolder("'#'表示序号")

	form.Append("*起始值", startEntry.Container)
	form.Append("宽度", widthEntry.Container)
	form.Append("*模式", patternEntry)
	form.OnSubmit = func() {
		window.tabCtx.Rules = append(window.tabCtx.Rules, model.NewSeqRule(startEntry.GetValue(0), widthEntry.GetValue(0), patternEntry.Text))
		onSubmit()
	}

	validator := func() {
		if startEntry.IsEmpty() {
			form.Disable()
			return
		}
		form.Enable()
	}
	validator()
	startEntry.OnChange = func(s string) {
		validator()
	}
	patternEntry.OnChanged = func(s string) {
		validator()
	}

	return container.NewTabItem("序列化", form)
}
